#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
const int N = 5e5 + 10;
int n, m;
struct node
{
	int a, b, w;
}s[N];
int f[N];
int find(int x)
{
	if (x != f[x]) {
		f[x] = find(f[x]);
	}
	return f[x];
}
int cmp(node a, node b)
{
	return a.w < b.w;
}
int kruskal()
{
	sort(s, s + m, cmp);
	for (int i = 1; i <= n; i++) {
		f[i] = i;
	}
	int res = 0;
	for (int i = 0; i < m; i++) {
		int a = s[i].a;
		int b = s[i].b;
		int w = s[i].w;
		int pa = find(a);
		int pb = find(b);
		if (pa != pb) {
			f[pa] = pb;
			res += w;
		}
	}
	return res;
}
int main()
{
	scanf("%d%d", &n, &m);
	for (int i = 0; i < m; i++) {
		int a, b, v;
		scanf("%d%d%d", &a, &b, &v);
		s[i] = { a,b,v };
	}
	cout << kruskal() << endl;
	return 0;
}